//
//  AddressCell.m
//  PigMarket
//
//  Created by mac on 2019/3/25.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AddressCell.h"

@interface AddressCell ()
/**
 name
 */
@property (nonatomic , strong) UILabel *nameLab;

/**
 phoneLab
 */
@property (nonatomic , strong) UILabel *phoneLab;

/**
 addressLab
 */
@property (nonatomic , strong) UILabel *addressLab;

/**
 默认图标
 */
@property (nonatomic , strong) UILabel *defaulLab;

/**
 地址类型标签图标
 */
@property (nonatomic , strong) UILabel *addressTypeLab;

/**
 修改图标
 */
@property (nonatomic , strong) UIButton *modifyBtn;


@end
@implementation AddressCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[AddressCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}
- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.phoneLab];
    [self.contentView addSubview:self.defaulLab];
    [self.contentView addSubview:self.addressTypeLab];
    [self.contentView addSubview:self.addressLab];
    [self.contentView addSubview:self.modifyBtn];
}

- (void)initLayout {
    // 布局子控件
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(22);
    }];
    [self.phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(110);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(24);
    }];
    [self.defaulLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.phoneLab.mas_right).offset(5);
        make.centerY.mas_equalTo(self.nameLab.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(34, 14));
    }];
    [self.addressTypeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.defaulLab.mas_right).offset(5);
        make.centerY.mas_equalTo(self.nameLab.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(34, 14));
    }];
    [self.addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.nameLab.mas_bottom).offset(7);
        make.right.mas_equalTo(-70);
        make.bottom.mas_equalTo(-15);
    }];
    [self.modifyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
}

#pragma mark - getter and setter
- (void)setAddressModel:(Address *)addressModel {
    _addressModel = addressModel;
    self.nameLab.text = addressModel.userName;
    self.phoneLab.text = addressModel.phone;
    self.addressLab.text = [NSString stringWithFormat:@"%@%@",addressModel.localName,addressModel.details];
    switch ([addressModel.isDefault intValue]) {
        case 0://普通
        {
            self.defaulLab.hidden = YES;
            [self.addressTypeLab mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.phoneLab.mas_right).offset(5);
                make.centerY.mas_equalTo(self.nameLab.mas_centerY);
                make.size.mas_equalTo(CGSizeMake(34, 14));
            }];
        }
            break;
        case 1://默认
        {
            self.defaulLab.hidden = NO;
            self.defaulLab.text = @"默认";
            [self.addressTypeLab mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.defaulLab.mas_right).offset(5);
                make.centerY.mas_equalTo(self.nameLab.mas_centerY);
                make.size.mas_equalTo(CGSizeMake(34, 14));
            }];
        }
            break;
        default:
            break;
    }
    if ([JTool iSNull:addressModel.tags]) {
        self.addressTypeLab.hidden = YES;
    }else {
        self.addressTypeLab.text = addressModel.tags;
        self.addressTypeLab.hidden = NO;
    }
    
//    if ([addressModel.tags isBlankString]) {//判断tag为空 则无标签显示
//        self.addressTypeLab.hidden = YES;
//    }else {
//        self.addressTypeLab.text = addressModel.tags;
//        self.addressTypeLab.hidden = NO;
//    }
}
- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        _nameLab.textColor = [UIColor textBlackColor];
        _nameLab.font = [UIFont largerFont];
    }
    return _nameLab;
}
- (UILabel *)phoneLab {
    if (!_phoneLab) {
        _phoneLab = [[UILabel alloc] init];
        _phoneLab.textColor = [UIColor textBlackColor];
        _phoneLab.font = [UIFont largerFont];
    }
    return _phoneLab;
}
- (UILabel *)defaulLab {
    if (!_defaulLab) {
        _defaulLab = [[UILabel alloc] init];
        _defaulLab.textColor = [UIColor whiteColor];
        _defaulLab.font = [UIFont systemFontOfSize:10];
        _defaulLab.textAlignment = NSTextAlignmentCenter;
        _defaulLab.backgroundColor = [UIColor colorWithHexString:@"#FFBF44"];
        _defaulLab.layer.cornerRadius = 7;
        _defaulLab.layer.masksToBounds = YES;
    }
    return _defaulLab;
}
- (UILabel *)addressTypeLab {
    if (!_addressTypeLab) {
        _addressTypeLab = [[UILabel alloc] init];
        _addressTypeLab.textColor = [UIColor whiteColor];
        _addressTypeLab.font = [UIFont systemFontOfSize:10];
        _addressTypeLab.textAlignment = NSTextAlignmentCenter;
        _addressTypeLab.backgroundColor = [UIColor primaryColor];
        _addressTypeLab.layer.cornerRadius = 7;
        _addressTypeLab.layer.masksToBounds = YES;
    }
    return _addressTypeLab;
}
- (UILabel *)addressLab {
    if (!_addressLab) {
        _addressLab = [[UILabel alloc] init];
        _addressLab.textColor = [UIColor colorWithHexString:@"#666666"];
        _addressLab.font = [UIFont smallFont];
        _addressLab.numberOfLines = 0;
    }
    return _addressLab;
}
- (UIButton *)modifyBtn {
    if (!_modifyBtn) {
        _modifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_modifyBtn setBackgroundImage:[UIImage imageNamed:@"user_address_modify"] forState:UIControlStateNormal];
    }
    return _modifyBtn;
}

@end
